import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        name: 'login',
        component: () => import('@/views/login/LoginView.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/HomeView.vue'),
        children: [
            {
                path: '/home/dashboard',
                name: 'dashboard',
                component: () => import('@/views/home/dashboard/DashboardView.vue')
            },
            {
                path: '/home/device',
                name: 'device',
                component: () => import('@/views/home/device/DeviceView.vue')
            },
            {
                path: '/home/fix',
                name: 'fix',
                component: () => import('@/views/home/fix/FixView.vue')
            },
            {
                path: '/home/keep',
                name: 'keep',
                component: () => import('@/views/home/keep/KeepView.vue'),
                children: [
                    {
                        path: '/home/keep/project',
                        name: 'keepProject',
                        component: () => import('@/views/home/keep/project/KeepProject.vue')
                    },
                    {
                        path: '/home/keep/plan',
                        name: 'keepPlan',
                        component: () => import('@/views/home/keep/plan/KeepPlan.vue')
                    }
                    
                ]
            },
            {
                path: '/home/patrol',
                name: 'patrol',
                component: () => import('@/views/home/patrol/PatrolView.vue')
            },
            {
                path: '/home/supplier',
                name: 'supplier',
                component: () => import('@/views/home/supplier/SupplierView.vue'),
                children: [
                    {
                        path: '/home/supplier/device',
                        name: 'device',
                        component: () => import('@/views/home/supplier/device/SupplierDevice.vue')
                    },
                    {
                        path: '/home/supplier/merchant',
                        name: 'merchant',
                        component: () => import('@/views/home/supplier/merchant/SupplierMerchant.vue')
                    }
                ]
            },
            {
                path: '/home/spare',
                name: 'spare',
                component: () => import('@/views/home/spare/SpareView.vue')
            },
            // 组织架构
            {
                path: '/home/group',
                name: 'group',
                component: () => import('@/views/home/group/GroupView.vue'),
                children: [
                    {
                        path: '/home/group/role',
                        name: 'role',
                        component: () => import('@/views/home/group/role/RoleView.vue')
                    },
                    {
                        path: '/home/group/user',
                        name: 'user',
                        component: () => import('@/views/home/group/user/UserView.vue')
                    }
                ]
            }
        ]
    },
    {
        path: '*',
        redirect: '/'
    }

]

const router = new VueRouter({
    routes,
    mode: 'history'
})

router.beforeEach((to, from, next) => {
    // console.log(to)
    // console.log(from)
    // console.log(next)
    if (!localStorage.getItem("token") && to.path !== '/') {
        next({ path: '/' })
    } else {
        next();
    }
})

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};
export default router;